package com.boot.controller;

import com.boot.entity.User;
import com.boot.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserControl {
    private static final Logger LOGGER = Logger.getLogger(UserControl.class);
    @Autowired
    private UserService userService;

    /*
    登陆
     */

    @RequestMapping("tologin")
    public String toLogin(){
        LOGGER.debug("to login-----------");
        return "login";
    }
    @RequestMapping("dologin")
    public  String doLogin(String username, String pwd, HttpSession session, HttpServletRequest request){
        LOGGER.debug("begin login----------");
        //System.out.println("userName:"+username);
        //System.out.println("pwd:"+pwd);

        User loginUser = userService.login(username);
         if(loginUser == null){
             request.setAttribute("loginerror","登陆失败！请检查您的用户名或者密码！");
                return "login";
        }
       else{
            if (pwd.equals(loginUser.getPwd())){
            // 加密：if (DigestUtils.md5Hex(pwd).equals(loginUser.getPwd())){
                //将用户名丢入
                session.setAttribute("loginUser",loginUser);
                //0.分公司管理员、1.燃料业务人员、2.监察人员、3.管理人员、4.公告通知发布人员
                if (loginUser.getRole() == 0) {
                    LOGGER.info("分公司管理员登陆成功!");
                    //return "admin/admin-index";
                    return "redirect:/";
                    //return  "redirect:findAllUser";
                }
                if (loginUser.getRole() == 1) {
                    LOGGER.info("燃料业务员员登陆成功!");
                    return "redirect:/";
                }
                if (loginUser.getRole() == 2) {
                    LOGGER.info("监察人员登陆成功!");
                    return "redirect:/";
                }
                if (loginUser.getRole() == 3) {
                    LOGGER.info("管理员登陆成功!");
                    return "redirect:/";
                }
                if (loginUser.getRole() == 4) {
                    LOGGER.info("公告通知发布人员登陆成功!");
                    return "redirect:/";
                }
            }
        }
        request.setAttribute("loginerror","登陆失败(╯︵╰)请检查您的用户名或者密码！");
        LOGGER.error("用户登陆失败，用户名或者密码错误！");
        return "login";
    }

    /*RequestMapping("findAllUser")
    public String findAllUsers(HttpServletRequest request){//将用户放入request
        //声明用户列表
        List<User> users = userService.findAllUser();
        request.setAttribute("users",users);

        return"admin/admin-index";
    }*/


    /*
    添加用户
     */
    @RequestMapping("adduser")
    public String addUser(User user,HttpServletRequest request){

        //判断用户名是否重复
        if(userService.checkUser(user.getUsername())){
            request.setAttribute("errormessage","用户名已被使用╥﹏╥ 换一个吧ヽ(ー_ー)ノ");

            return "forward:add_user";
            //return "forward:toadduser";
            //return "admin/add-user";
        }
        userService.addUser(user);
        request.setAttribute("message","添加用户成功(￣▽￣)~*");

        return "forward:query";
        //return "forward:findAllUser";
    }

    /*
    添加用户HTML页面
     *//*
    @RequestMapping("toadduser")
    public String toAddUser(){


        return "admin/add-user";
    }*/

}
